package com.laikan.reader.core.utils;

import android.text.TextUtils;
import com.socks.library.KLog;

import java.io.IOException;
import java.util.Properties;

/**
 * 将服务器连接地址写一个xxx.properties文件，利用此类去读取url地址
 *
 */
public class UrlManagerUtil {

    private static Properties properties = null;
    private static String LOG_TAG = "Config";

    static {
        try {
            properties = new Properties();
            properties.load(UrlManagerUtil.class.getResourceAsStream("/res/raw/mHtmlUrl.properties"));
        }catch (IOException ex) {
            KLog.e(LOG_TAG, "load mHtmlUrl.properties error:" + ex);
        }
    }

    public static String getString(String key, String defaultValue) {
        if (null != properties) {
            String result = properties.getProperty(key);
            if (!TextUtils.isEmpty(result)) {
                try {
                    return result;
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    KLog.e(LOG_TAG, "getString值出错：" + e);
                }
            }
        }
        return defaultValue;
    }

    public static int getInt(String key, int defaultValue) {
        String result = getString(key, String.valueOf(defaultValue));
        if (!TextUtils.isEmpty(result)) {
            try {
                return Integer.valueOf(result);
            }catch (NumberFormatException ex) {
                KLog.e(LOG_TAG, "getInt值出错：" + ex);
            }
        }
        return defaultValue;
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        String result = getString(key, String.valueOf(defaultValue));
        if (!TextUtils.isEmpty(result)) {
            try {
                return Boolean.valueOf(result);
            }catch (NumberFormatException ex) {
                KLog.e(LOG_TAG, "getBoolean值出错：" + ex);
            }
        }
        return defaultValue;
    }
}
